package com.ljx.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.listenter.ExcelSubjectListener;
import com.ljx.pojo.EduSubject;
import com.ljx.mapper.EduSubjectMapper;
import com.ljx.pojo.excel.ExcelSubjectData;
import com.ljx.pojo.subject.OneSubject;
import com.ljx.pojo.subject.TwoSubject;
import com.ljx.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 李加喜
 * @since 2020-09-24
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类  这个方法就是用来实现对Excel文件的读取与把数据添加到数据库里面的操作
    @Override
    public void saveSubject(MultipartFile file,EduSubjectServiceImpl eduSubjectService) {
        try {
            InputStream stream = file.getInputStream();
            EasyExcel.read(stream, ExcelSubjectData.class,new ExcelSubjectListener(eduSubjectService)).sheet().doRead();;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //显示全部的课程分类信息   也就是显示1级分类  在1级分类里面包含了2级分类
    @Override
    public List<OneSubject> getAllOneSubject() {
        //查询所有的一级分类 parent_id=0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        //ServiceImpl实现了将Mapper的自动注入  查询出1级
        List<EduSubject> listOne = baseMapper.selectList(wrapperOne);

        //查询所有的二级分类 parent_id!=0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id","0");
        //ServiceImpl实现了将Mapper的自动注入
        List<EduSubject> listTwo = baseMapper.selectList(wrapperTwo);

        //创建集合保存最后封装的数据
        ArrayList<OneSubject> finAllSubjects = new ArrayList<>();

        //封装一级分类
        for(int i=0;i<listOne.size();i++){
            //获取全部的数据 选择需要的数据进行添加
            EduSubject eduSubject = listOne.get(i);
            OneSubject oneSubject = new OneSubject();
            //将每一个1级分类的信息通过oneSubject进行传递给最后要显示的集合里面
            BeanUtils.copyProperties(eduSubject,oneSubject);
            ArrayList<TwoSubject> twoFinalSubject = new ArrayList<>();
            //遍历二级分类list集合
            for (int m=0;m<listTwo.size();m++){
                //获取每一个二级分类  这里里面是存在数据的
                EduSubject twoSubject = listTwo.get(m);
                //判断当前的二级是否属于遍历的这个1级分类的下面
                if (twoSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject1 = new TwoSubject();
                    BeanUtils.copyProperties(twoSubject,twoSubject1);
                    twoFinalSubject.add(twoSubject1);//保存所有符合条件的二级分类的信息
                }
            }
            //将当前1级分类下面的2级分类添加进来
            oneSubject.setChildren(twoFinalSubject);
            finAllSubjects.add(oneSubject);
        }
        return finAllSubjects;
    }
//    @Override
//    public void saveSubject(MultipartFile file) {
//        try {
//            InputStream stream = file.getInputStream();
//            EasyExcel.read(stream, ExcelSubjectData.class,new ExcelSubjectListener()).sheet().doRead();;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
